<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="数据源" prop="dataName">
            <el-select v-model="queryParams.dataName" filterable clearable placeholder="请选择/输入数据源名称" style="width: 200px">
              <el-option key="" label="全部" value="" />
              <el-option v-for="item in dataNameList" :key="item" :label="item" :value="item"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="表名称" prop="tableName">
            <el-input v-model="queryParams.tableName" placeholder="请输入表名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="表描述" prop="tableComment">
            <el-input v-model="queryParams.tableComment" placeholder="请输入表描述" clearable style="width: 200px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="创建时间" style="width: 308px">
            <el-date-picker
              v-model="dateRange"
              value-format="YYYY-MM-DD"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Download" @click="handleGenTable()" v-hasPermi="['tool:gen:code']">生成</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="info" plain icon="Upload" @click="openImportTable" v-hasPermi="['tool:gen:import']">导入</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleEditTable()" v-hasPermi="['tool:gen:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['tool:gen:remove']">
              删除
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" align="center" width="55"></el-table-column>
        <el-table-column label="序号" type="index" width="50" align="center">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="数据源" align="center" prop="dataName" :show-overflow-tooltip="true" />
        <el-table-column label="表名称" align="center" prop="tableName" :show-overflow-tooltip="true" />
        <el-table-column label="表描述" align="center" prop="tableComment" :show-overflow-tooltip="true" />
        <el-table-column label="实体" align="center" prop="className" :show-overflow-tooltip="true" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="160" />
        <el-table-column label="更新时间" align="center" prop="updateTime" width="160" />
        <el-table-column label="操作" align="center" width="330" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="界面预览" placement="top">
              <el-button link type="primary" icon="View" @click="handlePreview(scope.row)" v-hasPermi="['tool:gen:preview']"></el-button>
            </el-tooltip>
            <el-tooltip content="编辑" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleEditTable(scope.row)" v-hasPermi="['tool:gen:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['tool:gen:remove']"></el-button>
            </el-tooltip>
            <el-tooltip content="测试" placement="top">
              <el-button link type="primary" icon="Open" @click="handleTest(scope.row)" v-hasPermi="['tool:gen:test']"></el-button>
            </el-tooltip>
            <el-tooltip content="同步" placement="top">
              <el-button link type="primary" icon="Refresh" @click="handleSynchDb(scope.row)" v-hasPermi="['tool:gen:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="代码预览" placement="top">
              <el-button link type="primary" icon="View" @click="handleCodePreview(scope.row)" v-hasPermi="['tool:gen:preview']"></el-button>
            </el-tooltip>
            <el-tooltip content="生成代码" placement="top">
              <el-button link type="primary" icon="Download" @click="handleGenTable(scope.row)" v-hasPermi="['tool:gen:code']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>

    <!-- 预览界面 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="80%" top="5vh">
      <!-- 单表预览 -->
      <div v-if="previewData.type === 'single'">
        <el-form  label-width="120px">
          <el-row :gutter="20">
            <el-col
              v-for="field in previewData.mainFields"
              :key="field.columnName"
              :span="field.formSpan || 24"
            >
              <el-form-item :label="field.columnComment" :required="field.isRequired === '1'">
                <component
                  :is="getComponentType(field.htmlType)"
                  :model="previewData.mainForm[field.columnName]"
                  :key="field.columnName" :nm="field" @common-component="commonComponent"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <!-- 主子表预览 -->
      <div v-else-if="previewData.type === 'main-sub'">
        <!-- 主表部分 -->
        <el-card header="主表信息">
          <el-form  label-width="120px">
            <el-row :gutter="20">
              <el-col
                v-for="field in previewData.mainFields"
                :key="field.columnName"
                :span="field.formSpan || 24"
              >
                <el-form-item :label="field.columnComment" :required="field.isRequired === '1'">
                  <component
                    :is="getComponentType(field.htmlType)"
                    :model="previewData.mainForm[field.columnName]"
                    :key="field.columnName" :nm="field" @common-component="commonComponent"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>

        <!-- 子表部分 -->
        <el-card header="子表信息" class="mt-4">
          <div class="mb-4">
            <el-button type="primary" @click="addSubRow">+ 添加子项</el-button>
          </div>

          <el-table
            :data="previewData.subForms"
            border
            stripe
            class="sub-table"
          >
            <el-table-column
              v-for="field in previewData.subFields"
              :key="field.columnName"
              :prop="field.columnName"
              :label="field.columnComment"
              min-width="180"
            >
              <template #default="{ row, $index }">
                <component
                  :is="getComponentType(field.htmlType)"
                  v-model:model="row[field.columnName]"
                  :nm="field"
                  @common-component="commonComponent"
                />
              </template>
            </el-table-column>

            <el-table-column
              label="操作"
              width="80"
              fixed="right"
            >
              <template #default="{ $index }">
                <el-button
                  link
                  type="danger"
                  icon="Delete"
                  @click="removeSubRow($index)"
                />
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </div>

      <template #footer>
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">提交</el-button>
      </template>
    </el-dialog>
    <!-- 代码预览界面 -->
    <el-dialog :title="dialog.title" v-model="codeDialog.visible" width="80%" top="5vh" append-to-body class="scrollbar">
      <el-tabs v-model="codePreview.activeName">
        <el-tab-pane
          v-for="(value, key) in codePreview.data"
          :label="(key as any).substring((key as any).lastIndexOf('/') + 1, (key as any).indexOf('.vm'))"
          :name="(key as any).substring((key as any).lastIndexOf('/') + 1, (key as any).indexOf('.vm'))"
          :key="value"
        >
          <el-link :underline="false" icon="DocumentCopy" v-copyText="value" v-copyText:callback="copyTextSuccess" style="float:right"
            >&nbsp;复制</el-link
          >
          <pre>{{ value }}</pre>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
    <import-table ref="importRef" @ok="handleQuery" />
  </div>
</template>

<script setup name="online" lang="ts">
import { previewTable, delTable, genCode, synchDb } from '@/api/tool/gen';
import { list, getOnlineForm, getDataNames } from '@/api/online';
import { TableQuery, TableVO } from '@/api/online/types';
import importTable from './importTable.vue';
import router from '@/router';
import { ComponentInternalInstance } from 'vue';
import { ElForm, DateModelType } from 'element-plus';
import { useFlowable } from '@/views/workflow/hooks/useFlowable';
import { useOnline } from '@/views/workflow/hooks/useOnline'

const route = useRoute();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { getFormComponent } = useFlowable();
const { getComponentType } = useOnline()

const tableList = ref<TableVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const uniqueId = ref("");
const dataNameList = ref<Array<string>>([]);

const queryFormRef = ref(ElForm);
const importRef = ref(importTable);

const queryParams = reactive<TableQuery>({
    pageNum: 1,
    pageSize: 10,
    tableName: '',
    tableComment: '',
    dataName: ""
})

const onlPreview = reactive <any>({
    data: {}
})

// 预览数据结构
interface PreviewData {
  type: 'single' | 'main-sub' | null
  mainFields: OnlFieldVo[]
  subFields: OnlFieldVo[]
  mainForm: Record<string, any>
  subForms: Record<string, any>[]
  foreignKey: string
}

const previewData = reactive<PreviewData>({
  type: null,
  mainFields: [],
  subFields: [],
  mainForm: {},
  subForms: [],
  foreignKey: ''
})

const codePreview = ref <any>({
    data: {},
    activeName: 'domain.java'
})
const codeDialog = reactive<DialogOption>({
    visible: false,
    title: '代码预览'
});

const indexInner = ref(0);
const indexField = ref(0);
const olLabelCol = { xs: { span: 24 }, sm: { span: 5 } };
const olWrapperCol = { xs: { span: 24 }, sm: { span: 16 } };

const dialog = reactive<DialogOption>({
    visible: false,
    title: '界面预览'
});

const commonComponent = (nm) => {
  console.log('commonComponent nm=', nm);
  let fieldname = nm.columnName;
  onlPreview.data[fieldname] = nm.commonDataInfo;
};

//online表单提交数据信息
const submitOnlForm = () => {

}
const resetOnlForm = () => {

}
onActivated(() => {
  const time = route.query.t;
  if (time != null && time != uniqueId.value) {
      uniqueId.value = time as string;
      queryParams.pageNum = Number(route.query.pageNum);
      dateRange.value = ['', ''];
      queryFormRef.value.resetFields();
      getList();
  }
})

/** 查询多数据源名称 */
const getDataNameList = async () => {
  const res = await getDataNames()
  dataNameList.value = res.data;
}

/** 查询表集合 */
const getList = async () => {
    loading.value = true;
    const res = await list(proxy?.addDateRange(queryParams, dateRange.value));
    tableList.value = res.rows;
    total.value = res.total;
    loading.value = false;
}
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.pageNum = 1;
    getList();
}
/** 重置按钮操作 */
const resetQuery = () => {
    dateRange.value = ['', ''];
    queryFormRef.value.resetFields();
    handleQuery();
}

/** 代码预览按钮 */
const handleCodePreview = async (row: TableVO) => {
    const res = await previewTable(row.tableId);
    codePreview.value.data = res.data;
    codeDialog.visible = true;
    codePreview.value.activeName = 'domain.java';
}

/** 生成代码操作 */
const handleGenTable = async (row?: TableVO) => {
    const tbIds = row?.tableId || ids.value;
    if (tbIds == "") {
        proxy?.$modal.msgError('请选择要生成的数据');
        return;
    }
    if (row?.genType === "1") {
        await genCode(row.tableId);
        proxy?.$modal.msgSuccess('成功生成到自定义路径：' + row.genPath);
    } else {
        proxy?.$download.zip('/tool/gen/batchGenCode?tableIdStr=' + tbIds, 'ruoyi.zip');
    }
}
/** 同步数据库操作 */
const handleSynchDb = async (row: TableVO) => {
    const tableId = row.tableId;
    await proxy?.$modal.confirm('确认要强制同步"' + row.tableName + '"表结构吗？');
    await synchDb(tableId);
    proxy?.$modal.msgSuccess('同步成功');
}
/** 打开导入表弹窗 */
const openImportTable = () => {
    importRef.value.show(queryParams.dataName);
}

/** 预览按钮 */
const handlePreview = async (row: TableVO) => {
  try {
    // 清空旧数据
    previewData.type = null
    previewData.mainForm = {}
    previewData.subForms = []

    // 获取表单结构
    const formRes = await getOnlineForm(row.tableId)
    console.log('handlePreview formRes=', formRes)
    const formData = formRes.data

    // 判断表单类型
    if (formData.type === 'main-sub') {
      // 主子表处理
      previewData.type = 'main-sub'
      previewData.mainFields = formData.main.columnsVo
      previewData.subFields = formData.sub.columnsVo
      previewData.foreignKey = formData.foreignKey
    } else if (formData.type === 'single') {
      // 单表处理
      previewData.type = 'single'
      console.log('handlePreview formData=', formData)
      previewData.mainFields = formData.data.columnsVo
      console.log('handlePreview previewData.mainFields=', previewData.mainFields)
    }
    else if (formData.type === 'sub') {
      proxy?.$modal.msgError('附子表单类型不能预览！')
      return
    }

    dialog.visible = true
  } catch (error) {
    proxy?.$modal.msgError('数据加载失败')
  }
}

// 获取主键字段名
const getPkName = (tableId: number) => {
  const table = tableList.value.find(t => t.tableId === tableId)
  return table?.columnsVo?.find(col => col.isPk === '1')?.columnName || 'id'
}

// 添加子表行
const addSubRow = () => {
  previewData.subForms.push({})
}

// 删除子表行
const removeSubRow = (index: number) => {
  if (previewData.subForms.length > 1) {
    previewData.subForms.splice(index, 1)
  } else {
    proxy?.$modal.msgWarning('至少需要保留一个子项')
  }
}

// 提交处理
const handleSubmit = async () => {
  try {
    if (previewData.type === 'main-sub') {
      // 过滤空子项
      const validSubs = previewData.subForms.filter(sub =>
        Object.values(sub).some(v => v !== null && v !== undefined && v !== '')
      )

      // 添加外键关联
      const mainId = previewData.mainForm[getPkName(currentTableId.value)]
      validSubs.forEach(sub => sub[previewData.foreignKey] = mainId)

      await saveMainSubData({
        main: previewData.mainForm,
        subs: validSubs
      })
    } else {
      await saveSingleTable(previewData.mainForm)
    }

    proxy?.$modal.msgSuccess('操作成功')
    dialog.visible = false
    getList()
  } catch (error) {
    proxy?.$modal.msgError('操作失败')
  }
}
/** 复制代码成功 */
const copyTextSuccess = () => {
    proxy?.$modal.msgSuccess('复制成功');
}
// 多选框选中数据
const handleSelectionChange = (selection: TableVO[]) => {
    ids.value = selection.map(item => item.tableId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
}
/** 修改按钮操作 */
const handleEditTable = (row?: TableVO) => {
    const tableId = row?.tableId || ids.value[0];
    router.push({ path: '/online/edit/' + tableId, query: { pageNum: queryParams.pageNum } });
}
/** 删除按钮操作 */
const handleDelete = async (row?: TableVO) => {
    const tableIds = row?.tableId || ids.value;
    await proxy?.$modal.confirm('是否确认删除表编号为"' + tableIds + '"的数据项？');
    await delTable(tableIds);
    getList();
    proxy?.$modal.msgSuccess('删除成功');
}

/** online表单功能测试按钮操作 */
const handleTest = (row?: TableVO) => {
    if (row?.tplCategory === "sub") {
      proxy?.$modal.msgError('附子表不支持在线表单功能测试！');
      return;
    }
    console.log('handleTest row=', row);
    const tableId = row?.tableId;
    const tableName = row?.tableName;
    router.push({
      path: '/online/test/' + tableId + '/' + tableName
    });
}

onMounted(() => {
    getList();
    getDataNameList();
})
</script>
<style lang="scss" scoped>
  .olFromItem {
    width: 100%;
  }
  .sub-item {
    position: relative;
    margin-top: 20px;
    padding: 20px;
    border: 1px solid #ebeef5;
    border-radius: 4px;
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);
    }
  }

  .float-right {
    position: absolute;
    right: 20px;
    top: 20px;
  }

  .mt-4 {
    margin-top: 1rem;
  }

  .el-form-item.is-required .el-form-item__label::before {
    content: '*';
    color: #f56c6c;
    margin-right: 4px;
  }

  sub-table {
    margin-top: 15px;

    .el-table__cell {
      padding: 8px 0;
    }

    .cell {
      padding: 0 8px;
    }
  }

  // 调整组件内部间距
  .el-input, .el-select {
    width: 100%;
  }

</style>
